import Vue from 'vue'
import Router from 'vue-router'

const Login = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')

const Home = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')

const Welcome = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')

const Users = () =>
    import ( /* webpackChunkName:"user" */ '../components/user/Users.vue')

const Right = () =>
    import ( /* webpackChunkName:"power" */ '../components/power/Right.vue')

const Roles = () =>
    import ( /* webpackChunkName:"power" */ '../components/power/Roles.vue')


const Cate = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Cate.vue')

const Params = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Params.vue')

const goodsList = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/List.vue')


const Add = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Add.vue')


const Edit = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/editGoods.vue')


const Order = () =>
    import ( /* webpackChunkName:"Order" */ '../components/order/Order.vue')


const Report = () =>
    import ( /* webpackChunkName:"report" */ '../components/report/Report.vue')


Vue.use(Router)

const router = new Router({
        routes: [
            { path: '/', redirect: '/login' },
            { path: '/login', component: Login },
            {
                path: '/home',
                component: Home,
                redirect: '/welcome',
                children: [{
                    path: '/welcome',
                    component: Welcome
                }, {
                    path: '/users',
                    component: Users
                }, {
                    path: '/rights',
                    component: Right
                }, {
                    path: '/roles',
                    component: Roles
                }, {
                    path: '/categories',
                    component: Cate
                }, {
                    path: '/params',
                    component: Params
                }, {
                    path: '/goods',
                    component: goodsList
                }, {
                    path: '/goods/add',
                    component: Add
                }, {
                    path: '/goods/edit',
                    component: Edit
                }, {
                    path: '/orders',
                    component: Order
                }, {
                    path: '/reports',
                    component: Report
                }]
            }
        ]
    })
    // 路由导航守卫：防止未登录就可以访问权限页面
router.beforeEach((to, from, next) => {
    // 如果访问登录页面直接下一步
    if (to.path === '/login') return next()
        // 如果不是访问登录页面，先拿到浏览器内存的token值，
    const tokenData = window.sessionStorage.getItem('token')
        // 没有token值，就强制跳转到登录页面
    if (!tokenData) return next('/login')
        // 如果有token就表明已经登录 继续下一步
    next()
})
export default router